package com.cubeofnine.utils;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import com.cubeofnine.sugar.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityClass extends Activity implements ValueConstant {

	public static String errorMessage = "Unknown Error!";
	public static AlertDialog dialog;

	private static Context thisContext;

	private static CharSequence[] thisItems = null;
	public static final String MENU_QUIT = "Quit";
	public static final String MENU_LOGOUT = "Logout";

	public ActivityClass() {
		thisContext = this;
	}

	public static void gotoActivity(Class<?> cls) {
		gotoActivity(cls, null);
	}

	public static void gotoActivity(Class<?> cls, List<ActivityParams> params) {
		Intent intent = new Intent(thisContext, cls);
		if (params != null && !params.isEmpty()) {
			ActivityParams ap = null;
			Iterator<?> iter = params.iterator();
			while (iter.hasNext()) {
				ap = (ActivityParams) iter.next();
				if (ap.getValue() instanceof Boolean) {
					intent.putExtra(ap.getName(), (Boolean) ap.getValue());
				} else if (ap.getValue() instanceof Boolean[]) {
					intent.putExtra(ap.getName(), (Boolean[]) ap.getValue());
				} else if (ap.getValue() instanceof Byte) {
					intent.putExtra(ap.getName(), (Byte) ap.getValue());
				} else if (ap.getValue() instanceof Byte[]) {
					intent.putExtra(ap.getName(), (Byte[]) ap.getValue());
				} else if (ap.getValue() instanceof Character) {
					intent.putExtra(ap.getName(), (Character) ap.getValue());
				} else if (ap.getValue() instanceof Character[]) {
					intent.putExtra(ap.getName(), (Character[]) ap.getValue());
				} else if (ap.getValue() instanceof CharSequence) {
					intent.putExtra(ap.getName(), (CharSequence) ap.getValue());
				} else if (ap.getValue() instanceof CharSequence[]) {
					intent.putExtra(ap.getName(),
							(CharSequence[]) ap.getValue());
				} else if (ap.getValue() instanceof Double) {
					intent.putExtra(ap.getName(), (Double) ap.getValue());
				} else if (ap.getValue() instanceof Double[]) {
					intent.putExtra(ap.getName(), (Double[]) ap.getValue());
				} else if (ap.getValue() instanceof Float) {
					intent.putExtra(ap.getName(), (Float) ap.getValue());
				} else if (ap.getValue() instanceof Float[]) {
					intent.putExtra(ap.getName(), (Float[]) ap.getValue());
				} else if (ap.getValue() instanceof Integer) {
					intent.putExtra(ap.getName(), (Integer) ap.getValue());
				} else if (ap.getValue() instanceof Integer[]) {
					intent.putExtra(ap.getName(), (Integer[]) ap.getValue());
				} else if (ap.getValue() instanceof Long) {
					intent.putExtra(ap.getName(), (Long) ap.getValue());
				} else if (ap.getValue() instanceof Long[]) {
					intent.putExtra(ap.getName(), (Long[]) ap.getValue());
				} else if (ap.getValue() instanceof Parcelable) {
					intent.putExtra(ap.getName(), (Parcelable) ap.getValue());
				} else if (ap.getValue() instanceof Parcelable[]) {
					intent.putExtra(ap.getName(), (Parcelable[]) ap.getValue());
				} else if (ap.getValue() instanceof Serializable) {
					intent.putExtra(ap.getName(), (Serializable) ap.getValue());
				} else if (ap.getValue() instanceof Short) {
					intent.putExtra(ap.getName(), (Short) ap.getValue());
				} else if (ap.getValue() instanceof Short[]) {
					intent.putExtra(ap.getName(), (Short[]) ap.getValue());
				} else if (ap.getValue() instanceof String) {
					intent.putExtra(ap.getName(), (String) ap.getValue());
				} else if (ap.getValue() instanceof String[]) {
					intent.putExtra(ap.getName(), (String[]) ap.getValue());
				} else if (ap.getValue() instanceof Bundle) {
					intent.putExtras((Bundle) ap.getValue());
				} else if (ap.getValue() instanceof Intent) {
					intent.putExtras((Intent) ap.getValue());
				}
			}
		}

		thisContext.startActivity(intent);
	}

	public Bundle getIntentParams() {
		return getIntent().getExtras();
	}

	public void initProgressDialog(String message) {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.dialog_layout,
				(ViewGroup) findViewById(R.id.dialog_root));

		TextView text = (TextView) layout
				.findViewById(R.id.text_loading_message);

		if (message != null) {
			text.setText(message);
		}

		// ImageView image = (ImageView)
		// layout.findViewById(R.id.image_loading);
		// image.setImageResource(R.drawable.loading);

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(layout);
		dialog = builder.create();
		dialog.show();
	}

	public void alertMessage(int resId, boolean isLongTime) {
		alertMessage(this.getString(resId), isLongTime);
	}

	public void alertMessage(String message, boolean isLongTime) {
		int length = Toast.LENGTH_SHORT;

		if (isLongTime) {
			length = Toast.LENGTH_LONG;
		}

		Toast.makeText(this, message, length).show();
	}

	public void alertDialog(int resId) {
		alertDialog(this.getString(resId));
	}

	public void alertDialog(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(message)
				.setCancelable(false)
				.setNegativeButton(R.string.button_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	public void alertDialogForCloseApp(int resId) {
		alertDialogForCloseApp(this.getString(resId));
	}

	public void alertDialogForCloseApp(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(message)
				.setCancelable(false)
				.setNegativeButton(R.string.button_ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	public boolean isOnline() {
		boolean status = false;

		try {
			ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			// String msg = "";
			// for (NetworkInfo ni : cm.getAllNetworkInfo()) {
			// msg = "Type = " + ni.getType();
			// msg += "\nTypeName = " + ni.getTypeName();
			// msg += "\nSubtype = " + ni.getSubtype();
			// msg += "\nSubtypeName = " + ni.getSubtypeName();
			// msg += "\nState = " + ni.getState();
			// msg += "\nDetailedState = " + ni.getDetailedState();
			// alertDialog(msg);
			// }
			status = cm.getActiveNetworkInfo().isConnectedOrConnecting();
		} catch (Exception e) {
			status = false;
		}

		return status;
	}

	public int getScreenOrientation() {
		Display getOrient = getWindowManager().getDefaultDisplay();
		int orientation = Configuration.ORIENTATION_UNDEFINED;
		if (getOrient.getWidth() == getOrient.getHeight()) {
			orientation = Configuration.ORIENTATION_SQUARE;
		} else {
			if (getOrient.getWidth() < getOrient.getHeight()) {
				orientation = Configuration.ORIENTATION_PORTRAIT;
			} else {
				orientation = Configuration.ORIENTATION_LANDSCAPE;
			}
		}
		return orientation;
	}

	public void closeApplication() {
		finish();
		System.exit(0);
	}

	public void createMenuPopup(int resId, CharSequence[] items) {
		createMenuPopup(thisContext.getString(resId), items);
	}

	public void createMenuPopup(String title, CharSequence[] items) {
		thisItems = items;

		if (items == null) {
			thisItems = new CharSequence[] { MENU_QUIT };
		}

		AlertDialog.Builder builder = null;
		try {
			builder = new AlertDialog.Builder(thisContext);
			builder.setTitle(title);
			builder.setIcon(android.R.drawable.ic_menu_more);
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					((ActivityClass) thisContext)
							.onClickMenuPopupHandler(thisItems[item].toString());
				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		} catch (Exception e) {
			builder = new AlertDialog.Builder(this);
			builder.setTitle(title);
			builder.setIcon(android.R.drawable.ic_menu_more);
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int item) {
					((ActivityClass) thisContext)
							.onClickMenuPopupHandler(thisItems[item].toString());
				}
			});

			AlertDialog alert = builder.create();
			alert.show();
		}
	}

	public void onClickMenuPopupHandler(String menuSelected) {
		if (MENU_QUIT.equals(menuSelected)) {
			closeApplication();
		}
	}
}
